package com.iqiyi.pps.epg.core.model.control;

import com.iqiyi.pps.epg.api.model.web.broadcast.TPlatformControl;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

@Entity
@Table(name = "platform_control")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class PlatformControl  implements IPlatformControl {
    public static final int AVAILABLE_STATUS_ALLOW = 1;
    public static final int AVAILABLE_STATUS_DENY = 0;

    public static final String TW_BLACKLIST = "3_2_上海,3_1_北京,4_3001_广州,4_3003_深圳,3_4_天津,3_31_西藏,3_30_甘肃,3_26_云南,3_6_山东,3_3_广东,3_22_新疆,3_7_河北,3_10_浙江,3_19_江苏,3_29_青海,3_12_安徽,3_18_贵州,3_16_福建,3_11_湖北,3_28_宁夏,3_15_内蒙古,3_25_海南,3_17_辽宁,3_21_吉林,3_24_黑龙江,3_8_山西,3_13_陕西,3_20_四川,3_5_重庆,3_14_江西,3_27_湖南,6_301_海外,3_148_澳门,3_95_香港";
    public static final String NOT_TW_BLACKLIST = "3_228_台湾";

    private Long id;
    private Long fatherId;
    private Integer playPlatform;
    private String playPlatformVal;
    private Integer isDownloadAllowed;
    private Integer isDrm;
    private Integer isShareAllowed;
    private Integer isCooperationAllowed;
    private Integer availableStatus;
    private Integer rejectSearch;
    private Integer rejectRecommend;
    private Integer rejectTop;
    private Integer liveDelayTime;
    private Integer ipSegmentDelayTime;
    private String blacklist;
    private Timestamp addTime = new Timestamp(System.currentTimeMillis());
    private Timestamp updateTime;

    private int regionRejectSearch;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "father_id")
    public Long getFatherId() {
        return fatherId;
    }

    public void setFatherId(Long fatherId) {
        this.fatherId = fatherId;
    }

    @Column(name = "play_platform")
    public int getPlayPlatform() {
        return playPlatform;
    }

    public void setPlayPlatform(Integer playPlatform) {
        this.playPlatform = playPlatform;
    }

    @Transient
    public String getPlayPlatformVal() {
        return playPlatformVal;
    }

    public void setPlayPlatformVal(String playPlatformVal) {
        this.playPlatformVal = playPlatformVal;
    }

    @Column(name = "is_download_allowed")
    public Integer getIsDownloadAllowed() {
        return isDownloadAllowed;
    }

    public void setIsDownloadAllowed(Integer isDownloadAllowed) {
        this.isDownloadAllowed = isDownloadAllowed;
    }

    @Column(name = "is_drm")
    public Integer getIsDrm() {
        return isDrm;
    }

    public void setIsDrm(Integer isDrm) {
        this.isDrm = isDrm;
    }

    @Column(name = "is_share_allowed")
    public Integer getIsShareAllowed() {
        return isShareAllowed;
    }

    public void setIsShareAllowed(Integer isShareAllowed) {
        this.isShareAllowed = isShareAllowed;
    }

    @Column(name = "is_cooperation_allowed")
    public Integer getIsCooperationAllowed() {
        return isCooperationAllowed;
    }

    public void setIsCooperationAllowed(Integer isCooperationAllowed) {
        this.isCooperationAllowed = isCooperationAllowed;
    }

    @Column(name = "available_status")
    public int getAvailableStatus() {
        return availableStatus;
    }

    public void setAvailableStatus(Integer availableStatus) {
        this.availableStatus = availableStatus;
    }

    @Column(name = "reject_search")
    public int getRejectSearch() {
        return rejectSearch;
    }

    public void setRejectSearch(Integer rejectSearch) {
        this.rejectSearch = rejectSearch;
    }

    @Column(name = "reject_recommend")
    public Integer getRejectRecommend() {
        return rejectRecommend;
    }

    public void setRejectRecommend(Integer rejectRecommend) {
        this.rejectRecommend = rejectRecommend;
    }

    @Column(name = "reject_top")
    public Integer getRejectTop() {
        return rejectTop;
    }

    public void setRejectTop(Integer rejectTop) {
        this.rejectTop = rejectTop;
    }

    @Column(name = "live_delay_time")
    public int getLiveDelayTime() {
        return liveDelayTime;
    }

    public void setLiveDelayTime(Integer liveDelayTime) {
        this.liveDelayTime = liveDelayTime;
    }

    @Column(name = "ip_segment_delay_time")
    public Integer getIpSegmentDelayTime() {
        return ipSegmentDelayTime;
    }

    public void setIpSegmentDelayTime(Integer ipSegmentDelayTime) {
        this.ipSegmentDelayTime = ipSegmentDelayTime;
    }

    public String getBlacklist() {
        return blacklist;
    }

    public void setBlacklist(String blacklist) {
        this.blacklist = blacklist;
    }

    @Column(name = "add_time", updatable = false)
    public Timestamp getAddTime() {
        return addTime;
    }

    public void setAddTime(Timestamp addTime) {
        this.addTime = addTime;
    }

    @Column(name = "update_time", insertable = false, updatable = false)
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }
    @Column(name = "region_reject_search")
    public int getRegionRejectSearch() {
        return regionRejectSearch;
    }

    public void setRegionRejectSearch(int regionRejectSearch) {
        this.regionRejectSearch = regionRejectSearch;
    }

    public TPlatformControl entityToThrift() {
        TPlatformControl entity = new TPlatformControl();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        entity.setId(this.getId());
        entity.setFatherId(this.getFatherId());
        entity.setPlatform(this.getPlayPlatform());
        entity.setIsDownloadAllowed(this.getIsDownloadAllowed() == 0 ? false : true);
        entity.setIsDrm(this.getIsDrm() == 0 ? false : true);
        entity.setIsShareAllowed(this.getIsShareAllowed() == 0 ? false : true);
        entity.setIsCooperationAllowed(this.getIsCooperationAllowed() == 0 ? false : true);
        entity.setAvailableStatus(this.getAvailableStatus() == 0 ? false : true);
        entity.setRejectSearch(this.getRejectSearch() == 0 ? false : true);
        entity.setRejectRecommend(this.getRejectRecommend() == 0 ? false : true);
        entity.setRejectTop(this.getRejectTop() == 0 ? false : true);
        entity.setLiveDelayTime(this.getLiveDelayTime());
        entity.setIpSegmentDelayTime(this.getIpSegmentDelayTime());
        entity.setBlacklist(this.getBlacklist());
        entity.setRegionRejectSearch(this.getRegionRejectSearch() == 0 ? false:true);
        if (this.getAddTime() != null) {
            entity.setAddTime(sdf.format(this.getAddTime()));
        }
        if (this.getUpdateTime() != null) {
            entity.setUpdateTime(sdf.format(this.getUpdateTime()));
        }

        return entity;
    }

    public static PlatformControl thriftToEntity(TPlatformControl entity) {
        PlatformControl model = new PlatformControl();
        if (entity.getId() > 0) {
            model.setId(entity.getId());
        }
        model.setFatherId(entity.getFatherId());
        model.setPlayPlatform(entity.getPlatform());
        model.setIsDownloadAllowed(entity.isIsDownloadAllowed() ? 1 : 0);
        model.setIsDrm(entity.isIsDrm() ? 1 : 0);
        model.setIsShareAllowed(entity.isIsShareAllowed() ? 1 : 0);
        model.setIsCooperationAllowed(entity.isIsShareAllowed() ? 1 : 0);
        model.setAvailableStatus(entity.isAvailableStatus() ? 1 : 0);
        model.setRejectSearch(entity.isRejectSearch() ? 1 : 0);
        model.setRejectRecommend(entity.isRejectRecommend() ? 1 : 0);
        model.setRejectTop(entity.isRejectTop() ? 1 : 0);
        model.setLiveDelayTime(entity.getLiveDelayTime());
        model.setIpSegmentDelayTime(entity.getIpSegmentDelayTime());
        model.setBlacklist(entity.getBlacklist());
        model.setRegionRejectSearch( entity.isRegionRejectSearch() ? 1:0);
        if (StringUtils.isNotEmpty(entity.getAddTime())) {
            model.setAddTime(Timestamp.valueOf(entity.getAddTime()));
        }
        if (StringUtils.isNotEmpty(entity.getUpdateTime())) {
            model.setUpdateTime(Timestamp.valueOf(entity.getUpdateTime()));
        }

        return model;
    }
}
